// Package custom
// @Author Spume
// @Date 2024-12-25 14:58

package custom

import (
	"net/http"

	"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/errno"
	"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/log"
	"github.com/WeixinCloud/wxcloudrun-wxcomponent/comm/wx"
	"github.com/gin-gonic/gin"
)

type getPrivacySettingReq struct {
	PrivacyVer int `json:"privacy_ver,omitempty" wx:"privacy_ver"`
}

func getPrivacySettingHandler(c *gin.Context) {
	appid := c.DefaultQuery("appid", "")
	var req getPrivacySettingReq
	if err := c.ShouldBindJSON(&req); err != nil {
		log.Error(err.Error())
		c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
		return
	}
	resp, err := getPrivacySetting(appid, &req)
	if err != nil {
		c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
		return
	}
	c.JSON(http.StatusOK, errno.OK.WithData(resp))
}

type PrivacySetting struct {
	PrivacyList []string   `json:"privacy_list" wx:"privacy_list"` // 隐私列表
	SettingList []struct { // 设置列表
		PrivacyKey   string `json:"privacy_key" wx:"privacy_key"`     // 隐私键
		PrivacyText  string `json:"privacy_text" wx:"privacy_text"`   // 隐私文本
		PrivacyLabel string `json:"privacy_label" wx:"privacy_label"` // 隐私标签
	} `json:"setting_list" wx:"setting_list"`
	UpdateTime   int      `json:"update_time" wx:"update_time"` // 更新时间
	OwnerSetting struct { // 拥有者设置
		ContactPhone         string `json:"contact_phone" wx:"contact_phone"`                   // 联系电话
		ContactEmail         string `json:"contact_email" wx:"contact_email"`                   // 联系邮箱
		ContactQq            string `json:"contact_qq" wx:"contact_qq"`                         // 联系QQ
		ContactWeixin        string `json:"contact_weixin" wx:"contact_weixin"`                 // 联系微信
		StoreExpireTimestamp string `json:"store_expire_timestamp" wx:"store_expire_timestamp"` // 存储到期时间戳
		ExtFileMediaId       string `json:"ext_file_media_id" wx:"ext_file_media_id"`           // 扩展文件媒体ID
		NoticeMethod         string `json:"notice_method" wx:"notice_method"`                   // 通知方式
	} `json:"owner_setting" wx:"owner_setting"`
	PrivacyDesc struct { // 隐私描述
		PrivacyDescList []struct { // 隐私描述列表
			PrivacyKey  string `json:"privacy_key" wx:"privacy_key"`   // 隐私键
			PrivacyDesc string `json:"privacy_desc" wx:"privacy_desc"` // 隐私描述
		} `json:"privacy_desc_list" wx:"privacy_desc_list"`
	} `json:"privacy_desc" wx:"privacy_desc"`
	SdkPrivacyInfoList []struct { // SDK隐私信息列表
		SdkName    string     `json:"sdk_name" wx:"sdk_name"`         // SDK名称
		SdkBizName string     `json:"sdk_biz_name" wx:"sdk_biz_name"` // SDK业务名称
		SdkList    []struct { // SDK列表
			PrivacyKey   string `json:"privacy_key" wx:"privacy_key"`     // 隐私键
			PrivacyText  string `json:"privacy_text" wx:"privacy_text"`   // 隐私文本
			PrivacyLabel string `json:"privacy_label" wx:"privacy_label"` // 隐私标签
		} `json:"sdk_list" wx:"sdk_list"`
	} `json:"sdk_privacy_info_list" wx:"sdk_privacy_info_list"`

	PrivacyVer int `json:"privacy_ver,omitempty" wx:"privacy_ver"`
}

func getPrivacySetting(appid string, req *getPrivacySettingReq) (resp *PrivacySetting, err error) {
	_, body, err := wx.PostWxJsonWithAuthToken(appid, "/cgi-bin/component/getprivacysetting", "", *req)
	if err != nil {
		log.Error(err)
		return nil, err
	}
	resp = &PrivacySetting{}
	if err = wx.WxJson.Unmarshal(body, &resp); err != nil {
		log.Errorf("Unmarshal err, %v", err)
		return nil, err
	}
	return resp, nil
}

//type getPrivacySettingReq struct {
//	PrivacyVer int `json:"privacyVer,omitempty" wx:"privacy_ver"`
//}

func setPrivacySettingHandler(c *gin.Context) {
	appid := c.DefaultQuery("appid", "")
	var req PrivacySetting
	if err := c.ShouldBindJSON(&req); err != nil {
		log.Error(err.Error())
		c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
		return
	}
	err := setPrivacySetting(appid, &req)
	if err != nil {
		c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
		return
	}
	c.JSON(http.StatusOK, errno.OK)
}

func setPrivacySetting(appid string, req *PrivacySetting) (err error) {
	_, _, err = wx.PostWxJsonWithAuthToken(appid, "/cgi-bin/component/setprivacysetting", "", *req)
	if err != nil {
		log.Error(err)
		return
	}

	return
}

func uploadPrivacyExtFileHandler(c *gin.Context) {
	appid := c.DefaultQuery("appid", "")
	var req getPrivacySettingReq
	if err := c.ShouldBindJSON(&req); err != nil {
		log.Error(err.Error())
		c.JSON(http.StatusOK, errno.ErrInvalidParam.WithData(err.Error()))
		return
	}
	domainResp, err := getPrivacySetting(appid, &req)
	if err != nil {
		c.JSON(http.StatusOK, errno.ErrSystemError.WithData(err.Error()))
		return
	}
	c.JSON(http.StatusOK, errno.OK.WithData(domainResp))
}
